/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import entities.Action;
import entities.Categorie;
import entities.CategorySummary;
import entities.Month;
import entities.Year;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author chocoseb
 */
public class SummaryTableModel extends AbstractTableModel {

    private String[] _columnsNames = {
        "Categorie", "Total des revenus", "Total des dépenses", "Balance", "Moyenne", "+/-"
    };
    private ArrayList<CategorySummary> _listIncomes;

    public SummaryTableModel(Month m, Year y) {
        ArrayList<Categorie> list = Categorie.getCategorieList();
        _listIncomes = new ArrayList<CategorySummary>();
        for (Categorie categorie : list) {
            if (categorie.getParent() == null) {
                addCategorie(categorie);
            }
        }
        for (Action a : m.getActions()) {
            for (CategorySummary sum : _listIncomes) {
                
                if (sum.getCategorie().equals(a.getCategorie())) {
                    if (a.getAmount() > 0) {
                        sum.addIncome(a.getAmount());
                    } else {
                        sum.addExpenses(a.getAmount());
                    }
                }
            }
        }
    }

    private void addCategorie(Categorie categorie) {
        _listIncomes.add(new CategorySummary(categorie));
        for (Categorie c : categorie.getChildList()) {
            addCategorie(c);
        }
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public String getColumnName(int p_col) {
        String result;
        if (p_col < this.getColumnCount()) {
            result = _columnsNames[p_col];
        } else {
            result = "";
        }
        return result;
    }

    public int getColumnCount() {
        return _columnsNames.length;
    }

    public int getRowCount() {
        return _listIncomes.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object result = null;
        switch (columnIndex) {
            case 0:
                result = _listIncomes.get(rowIndex).getCategorie();
                break;
            case 1:
                result = _listIncomes.get(rowIndex).getIncomes();
                break;
            case 2:
                result = _listIncomes.get(rowIndex).getExpenses();
                break;
            case 3:
                result = _listIncomes.get(rowIndex).getBalance();
                break;
            case 4:
                result = _listIncomes.get(rowIndex).getAverage();
                break;
            case 5:
                result = _listIncomes.get(rowIndex).getDifferenceFromAverage();
                break;
        }
        return result;
    }
}
